<?php

class TagModel extends Model {
	
	
    public function index(){
		return 1;
    }
    
    /**
     * 添加Tag
     * 若存在 count ++
     * @param string $tagName
     * @return int $tagId
     */
    function addTag($tagName) {
    	$con = array('tagname' => $tagName);
    	$haveTag = $this->field('id,count')->where($con)->find();
    	if($haveTag) {
    		$data = array();
    		$data['id'] = $haveTag['id'];
			$data['count'] = $haveTag['count'] + 1;
    		$this->save($data);
			$tagId = $haveTag['id'];
    	} else {
			$tagId = $this->add();
		}
		return $tagId;
    }
    
    /**
     * 由ID获取Tag
     * @param int $tagId
     * @return string $tagName
     */
    function getOne($tagId) {
		$tag = $this->find($tagId);
		return $tag;
	}
	
	function getList($condition = array(), $limit = 15, $order = '') {
		$list = $this->where($condition)->order($order)->limit($limit)->select();
		return $list;
	}
	
	//tag count --
	function tagDec($tagIds) {
		if(!$tagIds) {
			return false;
		}
		$data = array();
		if(is_array($tagIds)) {
			$data['id'] = array('in', $tagIds);
		} else {
			$data['id'] = $tagIds;
		}
		$data['count'] = array('exp','count-1');
		return $this->save($data);
	}
	
	
}